//
// Created by 抑~风 on 2022/11/29.
//

#include<fstream>
#include<iostream>
#include "regexp.h"
using namespace std;
namespace CWJ{
    bool CompilerUtil::isBlank(char c){
        return c == ' ' || c == '\t' || c == '\n';
    }

    bool CompilerUtil::isAnnotation(const string& str){
        return str.size()>=2 && str[0] == '/' && str[1] == '/';
    }

    bool CompilerUtil::trim(string& line){
        int start = 0,ed = line.size() - 1;
        while(start < line.size() && isBlank(line[start])) start ++;
        while(ed >= 0 && isBlank(line[ed])) ed--;
        if(start > ed)  return false;
        line = line.substr(start,ed-start+1);
        return true;
    }

    bool CompilerUtil::readRule(const string& path,vector<string>&rules) {
        ifstream in(path, ios::in);
        string line,buf;
        while(getline(in,line)){
            if(!trim(line) || isAnnotation(line))  continue;
            buf += line;
            if(line.back() == ';') {
                buf.pop_back();
                rules.push_back(std::move(buf));
                buf.clear();
            }else  buf += " ";
        }
        return true;

    }




}